/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package shell;

import games.Heuristic;
import games.KamisadoState;
import games.KamisadoState.KamisadoMove;
import games.MatchKamisadoState;
import java.util.List;
import java.util.Random;
import minmax.GameOutcome;
import minmax.MinMax;

/**
 *
 * @author Mikael
 */
public class KamisadoBenchmark {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here

        Heuristic<KamisadoState> heuristic1 = getHeuristic(args[0]);
        Heuristic<KamisadoState> heuristic2 = getHeuristic(args[1]);

        ResultKeeper keeper = new ResultKeeper();
        int limit = Integer.parseInt(args[2]);
        for (int i = 0; i < limit; i++) {
            KamisadoState state = new MatchKamisadoState(heuristic1, heuristic2);
            runGame(state, keeper);
        }
        System.out.println(keeper.toString());

        keeper = new ResultKeeper();
        for (int i = 0; i < limit; i++) {
            KamisadoState state = new MatchKamisadoState(heuristic1, heuristic2);
            runGame(state, keeper);
        }
        System.out.println(keeper.toString());

    }

    private static Heuristic<KamisadoState> getHeuristic(String name) {
        String className = "games.Kamisado." + name + "Heuristic";
        try {
            Class cl = Class.forName(className);
            java.lang.reflect.Constructor co = cl.getConstructor((Class[]) null);
            return (Heuristic<KamisadoState>) co.newInstance((Object[]) null);
        } catch (Exception ex) {
            return null;
        }
    }

    private static GameOutcome runGame(KamisadoState state) {
        MinMax algo = new MinMax(10);
        Random random = new Random();
        while (!state.isFinished()) {
            List<KamisadoMove> steps = algo.minmaxList(state);
            state.move(steps.get(random.nextInt(steps.size())));
        }

        return state.outcome();
    }

    private static void runGame(KamisadoState state, ResultKeeper keeper) {
        GameOutcome result = runGame(state);
        switch (result) {
            case Draw:
                keeper.draw++;
                break;
            case First:
                keeper.player1++;
                break;
            case Second:
                keeper.player2++;
                break;
            default:
                throw new AssertionError("unknown constant");
        }
    }

    private static class ResultKeeper {

        int player1;
        int player2;
        int draw;

        @Override
        public String toString() {
            return player1 + ";" + draw + ";" + player2;
        }
    }
}
